#include "BlockQueue.hpp"
#include "Task.hpp"
#include <sys/types.h>
#include <ctime>

void* productor(void* _bq)
{
    BlockQueue<CalTask>* bq = static_cast<BlockQueue<CalTask>*>(_bq);

    while(1)
    {
        int x = rand() % 10 + 1;
        int y = rand() % 5 + 1;
        int operCode = rand() % oper.size();
        CalTask t(x, y, oper[operCode], mymath);
        sleep(1);
        bq->push(t);
        std::cout << "productor thread, 生产计算任务 : " << t.toTaskString() << std::endl;
    }
    return nullptr;
}

void* consumer(void* _bq)
{
    BlockQueue<CalTask>* bq = static_cast<BlockQueue<CalTask>*>(_bq);
    while(1)
    {
        CalTask t;
        bq->pop(&t);

        std::string result = t();
        std::cout << "cal thread, 完成计算任务 : " << result << "  done...." << std::endl;
    }
    return nullptr;
}


int main()
{
    BlockQueue<CalTask>* bq = new BlockQueue<CalTask>;
    pthread_t c, p; 
    pthread_create(&c, nullptr, productor, bq);
    pthread_create(&p, nullptr, consumer, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    delete bq;
    return 0;
}